package com.avic.rong.activity.mine;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.avic.rong.BaseActivity;
import com.avic.rong.R;
import com.avic.rong.api.mine.UpEmailApi;
import com.avic.rong.utils.SharedPreferencesUtil;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.ToastTools;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;
import com.luck.picture.lib.dialog.CustomDialog;

import butterknife.BindView;

//账号设置-个人信息-修改邮箱
public class UpEmailActivity extends BaseActivity {

    @BindView(R.id.edittext_email)
    EditText edittext_email;
    private String login_token;
    private String email;
    private Intent intent;
    private String oldemail;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_up_email;
    }

    @Override
    protected void initView() {
        tvTitle.setText("修改邮箱");
        tvRight.setText("确定");
        tvRight.setTextColor(getResources().getColor(R.color.white));

        if (SharedPreferencesUtil.isLogin()) {
            login_token = SharedPreferencesUtil.getLoginToken();
        }

        intent = getIntent();
        oldemail = intent.getStringExtra("email");
        edittext_email.setText(oldemail);


    }

    private void showDialog() {
        CustomDialog dialog = new CustomDialog(this, R.layout.dialog_upname);
        dialog.show();
        dialog.setCanceledOnTouchOutside(false);
        dialog.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });



        dialog.findViewById(R.id.tv_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    @Override
    protected void addListener() {
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String trim = edittext_email.getText().toString().trim();
                if (trim.equals(oldemail)){
                    finish();
                }else {
                    showDialog();
                }
            }
        });

        tvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        edittext_email.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                tvRight.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (s.length()!=0) {
                            upEmail();
                        }else {
                            finish();
                        }
                    }
                });
            }
        });

    }


    public static Intent queryIntent(Context context) {
        Intent intent = new Intent(context, UpEmailActivity.class);
        return intent;
    }

    private void upEmail() {
        email = edittext_email.getText().toString().trim();
        String email_ruler = "^[a-z0-9]+([._\\\\\\\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$";
        if (!email.matches(email_ruler)){
            Toast.makeText(this, "邮箱格式不正确", Toast.LENGTH_SHORT).show();
            return;
        }
        UpEmailApi upEmailApi = new UpEmailApi(this, upEmailListener);
        upEmailApi.setArgument(login_token, email);
        HttpManager.getInstance().doHttpDeal(upEmailApi);
    }

    HttpOnNextListener<BaseResultEntity> upEmailListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            if (baseResultEntity.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)) {
                ToastTools.showPublicToast("修改成功");
                intent.putExtra("email_new", email);
                setResult(200, intent);
                finish();
            } else {
                Toast.makeText(UpEmailActivity.this, baseResultEntity.getMsg(), Toast.LENGTH_SHORT).show();
            }
        }
    };

}
